import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/homePage',
    component: () => import('@/views/homePage/index'),
    hidden: true
  },
  {
    path: '/map',
    component: () => import('@/views/map/index'),
    hidden: true
  },
  {
    path: '/regionMap',
    component: () => import('@/views/map/region'),
    hidden: true
  },
  {
    path: '/communityMap',
    component: () => import('@/views/map/community'),
    hidden: true
  },
  {
    path: '/streetMap',
    component: () => import('@/views/map/street'),
    hidden: true
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  }
]
export const asyncRoutes = [
  // 需要权限的路由
  // {
  //   path: '/example',
  //   component: Layout,
  //   redirect: '/example/table',
  //   name: 'Example',
  //   meta: { title: 'Example', icon: 'el-icon-s-help' },
  //   hidden: true,
  //   children: [
  //     {
  //       path: 'table',
  //       name: 'Table',
  //       component: () => import('@/views/table/index'),
  //       meta: { title: 'Table', icon: 'table' }
  //     }
  //   ]
  // },
  {
    path: '/regionAdmin',
    component: Layout,
    redirect: '/regionAdmin',
    alwaysShow: true, // will always show the root menu
    name: 'Example',
    meta: { title: '区域管理员', icon: 'el-icon-user-solid', roles: ['0'] },
    children: [
      {
        path: 'communityTypist',
        name: 'CommunityTypist',
        component: () => import('@/views/regionAdmin/communityTypist/index'),
        meta: { roles: ['0'], title: '小区录入员', icon: 'form' }
      },
      {
        path: 'regionCheck',
        name: 'RegionCheck',
        component: () => import('@/views/regionAdmin/regionCheck/index'),
        meta: { roles: ['0'], title: '区审核员', icon: 'el-icon-s-opportunity' }
      },
      {
        path: 'streetCheck',
        name: 'StreetCheck',
        component: () => import('@/views/regionAdmin/streetCheck/index'),
        meta: { roles: ['0'], title: '街道审核员', icon: 'el-icon-s-opportunity' }
      }
    ]
  },
  {
    path: '/nested',
    component: Layout,
    redirect: '/nested/menu1',
    name: 'Nested',
    meta: {
      title: '患者',
      icon: 'nested',
      roles: [ '3', '2', '1']
    },
    children: [
      {
        path: 'menu1',
        component: () => import('@/views/nested/menu1/index'), // Parent router-view
        name: 'Menu1',
        meta: { title: '已审核', roles: [ '3', '2', '1'] },
        children: [
          {
            path: 'menu1-1',
            component: () => import('@/views/nested/menu1/menu1-1'),
            name: 'Menu1-1',
            meta: { title: '被通过', roles: [ '3', '2'] }
          },
          {
            path: 'menu1-3',
            component: () => import('@/views/nested/menu1/menu1-3'),
            name: 'Menu1-3',
            meta: { title: '通过', roles: [ '2', '1'] }
          },
          {
            path: 'menu1-2',
            component: () => import('@/views/nested/menu1/menu1-2'),
            name: 'Menu1-2',
            meta: { title: '被驳回', roles: [ '3', '2'] }
          },
          {
            path: 'menu1-4',
            component: () => import('@/views/nested/menu1/menu1-4'),
            name: 'Menu1-4',
            meta: { title: '驳回', roles: ['2', '1'] }
          }
        ]
      },
      {
        path: 'menu2',
        component: () => import('@/views/nested/menu2/index'),
        name: 'Menu2',
        meta: { title: '待审核', roles: [ '3', '2', '1'] }
      }
    ]
  },
  {
    path: '/shelters',
    component: Layout,
    redirect: '/shelters/choosen',
    name: 'Shelters',
    meta: {
      title: '患者',
      icon: 'nested',
      roles: [ '5', '4' ]
    },
    children: [
      {
        path: 'choosen',
        component: () => import('@/views/shelters/choosen'), // Parent router-view
        name: 'Choosen',
        meta: { title: '选择患者', roles: ['5', '4'] }
      },
      {
        path: 'passed',
        component: () => import('@/views/shelters/passed'),
        name: 'Passed',
        meta: { title: '方舱患者', roles: ['5'] }
      }
    ]
  },

  {
    path: '/preAdmin',
    component: Layout,
    meta: { roles: ['0'] },
    children: [
      {
        path: 'index',
        name: 'PreAdmin',
        component: () => import('@/views/preAdmin/index'),
        meta: { title: '防疫管理员', icon: 'el-icon-s-home', roles: ['0'] }
      }
    ]
  },

  {
    path: '/region',
    component: Layout,
    meta: { roles: ['0'] },
    children: [
      {
        path: 'index',
        name: 'Region',
        component: () => import('@/views/region/index'),
        meta: { title: '区域管理', icon: 'el-icon-school', roles: ['0'] }
      }
    ]
  },

  // {
  //   path: '/region',
  //   component: () => import('@/views/region/index'),
  //   name: 'Region',
  //   meta: { title: '区域管理', icon: 'el-icon-school', roles: ['0'] },
  // },

  {
    path: '/preRegion',
    component: Layout,
    meta: { roles: ['0'] },
    children: [
      {
        path: 'index',
        name: 'PreRegion',
        component: () => import('@/views/preRegion/index'),
        meta: { title: '防疫管理', icon: 'el-icon-office-building', roles: ['0'] }
      }
    ]
  },
 
  {
    path: '/menu',
    component: Layout,
    redirect: '/menu/menuManage',
    name: 'Menu',
    meta: {
      title: '基础管理',
      icon: 'el-icon-s-help',
      roles: ['0']
    },
    children: [
      {
        path: 'menuManage',
        component: () => import('@/views/menu/menuManage/index'),
        name: 'MenuManage',
        meta: { title: '菜单管理', icon: 'nested', roles: ['0'] }
      },
      {
        path: 'rolePower',
        component: () => import('@/views/menu/rolePower/index'),
        name: 'RolePower',
        meta: { title: '角色权限管理', icon: 'el-icon-menu', roles: ['0'] },
      }
    ]
  },

  {
    path: 'external-link',
    component: Layout,
    meta: { roles: ['5', '4', '3', '2', '1', '0'] },
    children: [
      {
        path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
        meta: { title: 'External Link', icon: 'link', roles: ['5', '4', '3', '2', '1', '0'] }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'hash', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  // console.log(newRouter.matcher);
  router.matcher = newRouter.matcher // reset router
}

export default router
